package com.test.daily.leetcode.y2021.m12.day1211;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2021/12/11 上午 09:47
 * @version: 1.0
 */
public class Code01_TopVotedCandidate {
    HashMap<Integer, Integer> countMap;
    ArrayList<Integer> topTimes;
    int[] times;

    public Code01_TopVotedCandidate(int[] persons, int[] times) {
        countMap = new HashMap<>();
        topTimes = new ArrayList<>();
        int top = -1;
        countMap.put(-1, -1);
        for (int i = 0; i < persons.length; i++) {
            int p = persons[i];
            countMap.put(p, countMap.getOrDefault(p, 0) + 1);
            if (countMap.get(p) >= countMap.get(top)) {
                top = p;
            }
            topTimes.add(top);
        }
        this.times = times;
    }


    public int q(int t) {
        int L = 0;
        int R = times.length - 1;
        while (L < R) {
            int mid = L + (R - L + 1) / 2;
            if (times[mid] <= t) {
                L = mid;
            } else {
                R = mid - 1;
            }
        }
        return topTimes.get(L);
    }
}
